#!/usr/bin/env python
# The intermecdiate lexicon is necessary to generate before applying the post lexical rules.
# This module replaces the entries in the basic lexicon, with the entries in the exception
# dictionaries.

import towns
import exceptions_dicts
import config
import pdb; pdb.set_trace()

# None of this code works.  I've banged my head against why this could be, to no avail.
# I've sectioned off the first function.  As you can see, I try to pass it dictionaries 
# from towns.py and exceptions_dicts.py.  If a key in the country dict matches the 'COUNTRY' value 
# in the towns dict 'guk', then make all the values for that country key a list called 'country_exceptions'.
# If no match is found, then there are no country exceptions, return an empty list.
# Unfortunately, when I run this function for bug checking 'country_exceptions' does not
# get defined.  I have the same problem with the region and town functions.  But they are
# basically the same functions.  So, if i fix the first function i can fix the second two.
def get_country_exceptions(town, country):          
    """Given a town code, get the country exceptions"""                               
    for key in country.keys():                                                        
        if key == towns.guk['COUNTRY']:
            country_exceptions = country[key]
        else:
            country_exceptions = []
    return country_exceptions
    pdb.set_trace()

def main():
   town = towns.guk
   country = exceptions_dicts.COUNTRY
   country_exceptions = get_country_exceptions(town, country)
if __name__ == 'main':
    main()




########################################################################################
def get_region_exceptions(_guk):
    # Given a town code, get the region exceptions."""
    for key in _REGION.keys():
       if key == _guk['REGION']:
           region_exceptions = [value for value in _guk['REGION']]
    return region_exceptions

def get_towns_exceptions(_guk):
    # Given a town code, get the towns exceptions"""
    for key in _TOWN.keys():
        if key == _guk['TOWN']:
            towns_exceptions = [value for value in _guk['TOWN']]
    return towns_exceptions

def add_all_exceptions(country_exceptions, region_exceptions, town_exceptions):
    # Given the country, region, and town exceptions, add them 
    # all together to get all exceptions for a given town code."""
    all_exceptions = country_exceptions + region_exceptions + town_exceptions
    return all_exceptions


def delete_unexceptables(all_exceptions, PATH_TO_UNILEX):
    # Given all the exceptions and the basic lexicon, delete
    # basic entries that will be replaced by exceptional entries."""
    # Put Unilex in a list so that 
    # it is ready to ammend:
    unilex = []
    with open(PATH_TO_UNILEX) as f:
        for line in f.readlines():
            unilex.append(line)
    # Create a list of words to check 
    # and see which entries need replacing:
    words = [line.split(':', 1)[0] for line in all_exceptions]
    # Set the base case for when 
    # all basic entries that have an 
    # exception have been deleted:
    for entry in unilex:
        x = entry.index(':', 1)
        if entry[:x] not in unilex:
            return unilex
        # Delete a basic entry and pass unilex
        # back to the function:
        else:
            if entry[:x] in words:
                unusable = unilex.index(entry)
                unilex.pop(unuable)
                unilex = delet_unexceptables(all_exceptions, unilex)
        
def get_intermediate_lexicon(all_exceptions, unilex):
    # Given the basic lexicon, cleaned of all exceptional cases,
    # add the exceptional entries to generate a complete intermediate lexicon."""
    intermediate_lexicon = all_exceptions + unilex
    return intermediate_lexicon

""" 
def main():
    country_exceptions = get_country_exceptions(guk, country)
    region_exceptions = get_region_exceptions(_guk)
    towns_exceptions = get_towns_exceptions(_guk)
    all_exceptions =  add_all_exceptions(country_exceptions, region_exceptions, town_exceptions)
    unilex = delete_unexceptables(all_exceptions, PATH_TO_UNILEX)
    intermediate_lexicon = get_intermediate_lexicon(all_exceptions, unilex)

       
if __name__ == 'main':
    main()"""
